package com.ray.server;
import java.sql.*;

public class Login {
	private Connection conn = null;
	private ResultSet rs = null;
	private Statement stmt;

	public Login() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/test", "root", "1234");
			stmt = conn.createStatement();
		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("chatreg():" + e.getMessage());
		} catch (java.sql.SQLException e) {
			System.err.println("chatreg():" + e.getMessage());
		}
	}

	public ResultSet executeQuery(String sql) {
		rs = null;
		try {
			if (conn == null) {
				conn = DriverManager.getConnection(
						"jdbc:mysql://127.0.0.1:3306/test", "root", "1234");
				stmt = conn.createStatement();
			}
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		return rs;
	}

	public boolean executeUpdate(String sql) {
		boolean success = false;
		try {
			if (conn == null) {
				conn = DriverManager.getConnection(
						"jdbc:mysql://127.0.0.1:3306/test", "root", "1234");
				stmt = conn.createStatement();
			}
			stmt.executeUpdate(sql);
			success = true;
		} catch (SQLException e) {
			success = false;
		}
		return success;
	}

	public void close() {
		try {
			if (!conn.isClosed())
				conn.close();
			conn = null;
		} catch (SQLException el) {
			el.printStackTrace();
		}
	}
}
